package com.contexticon.TableEditorsRenderers;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerListModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;


public class SpinnerRenderer extends AbstractCellEditor
implements TableCellRenderer {
final JSpinner spinner = new JSpinner();

// Initializes the spinner.
public SpinnerRenderer(String[] items) {
spinner.setModel(new SpinnerListModel(java.util.Arrays.asList(items)));
}

// Prepares the spinner component and returns it.
public Component getTableCellEditorComponent(JTable table, Object value,
    boolean isSelected, int row, int column) {
spinner.setValue(value);
return spinner;
}

// Enables the editor only for double-clicks.
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
    return ((MouseEvent)evt).getClickCount() >= 2;
}
return true;
}

// Returns the spinners current value.
public Object getCellEditorValue() {
return spinner.getValue();
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
		boolean isSelected, boolean hasFocus, int row, int column) {
	// TODO Auto-generated method stub
	spinner.setValue(value);
	return spinner;
}
}